import { defineStore } from "pinia";
import { reqLogin, reqLogout } from "@/api/register";
const initState = JSON.parse(localStorage.getItem("user")) || {
  token: "",
  nickName: "",
};

export const useUserInfoStore = defineStore({
  id: "user",
  state: () => ({
    initState,
  }),
  actions: {
    // 1. 获取用户信息
    async login({ phone, password }) {
      const user = await reqLogin(phone, password);
      // console.log(user);
      this.initState = user;
      // 将数据储存在本地储存上
      const { nickName, token } = user;
      localStorage.setItem("user", JSON.stringify({ nickName, token }));
    },
    // 2. 退出登录
    async logOut() {
      this.initState = "";
      localStorage.removeItem("user");
    },
  },
  getters: {},
});
